/*
 * $Id$
 *
 * Copyright 2008 HuangHuang. All rights reserved.
 */
package com.crm.web.util;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.tapestry.form.IPropertySelectionModel;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author <a href="mailto:huang0510@hotmail.com">HuangHuang</a>
 * @version $Id$
 */
public class PojoPropertySelectionModel implements IPropertySelectionModel {

    protected List list;
    protected String labelField;
    protected String valueField;

    /**
     * Build an empty property selection model.
     */
    public PojoPropertySelectionModel() {
        this(Arrays.asList(), null, null);
    }

    /**
     * Build a pojo property selection model.
     *
     * @param list       The list
     * @param labelField The label field
     * @param valueField The value field
     */
    public PojoPropertySelectionModel(List list, String labelField, String valueField) {
        this.list = list;
        this.labelField = labelField;
        this.valueField = valueField;
    }

    /**
     * Build a pojo property selection model.
     *
     * @param c          Collection
     * @param labelField The label field
     * @param valueField The value field
     */
    public PojoPropertySelectionModel(Collection c, String labelField, String valueField) {
        list = new ArrayList(c);
        this.labelField = labelField;
        this.valueField = valueField;
    }

    /**
     * Get the number of options.
     *
     * @return option count
     */
    public int getOptionCount() {
        return list.size();
    }

    /**
     * Get the option at index.
     *
     * @param index Index
     * @return object Object at index
     */
    public Object getOption(int index) {
        if (index > (list.size() - 1)) {
            return null;
        }

        return list.get(index);
    }

    /**
     * Get the label at index.
     *
     * @param index Index
     * @return label Label at index
     */
    public String getLabel(int index) {
        Object obj = list.get(index);

        try {
            return BeanUtils.getProperty(obj, labelField);
        } catch (Exception e) {
            throw new RuntimeException("Error getting property label", e);
        }
    }

    /**
     * Get the value at index.
     *
     * @param index Index
     * @return value Value at index
     */
    public String getValue(int index) {
        Object obj = list.get(index);
        try {
            return BeanUtils.getProperty(obj, valueField);
        } catch (Exception e) {
            throw new RuntimeException("Error getting property value", e);
        }
    }

    public boolean isDisabled(int index) {
        return false;
    }

    /**
     * Translate value to object.
     *
     * @param value Value
     * @return object Object from value
     */
    public Object translateValue(String value) {
        if (value == null) {
            return null;
        }

        return value; 
    }

    public String toString() {
        return new ToStringBuilder(this).
                append("list", list).
                append("labelField", labelField).
                append("valueField", valueField).
                toString();
    }

    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PojoPropertySelectionModel)) return false;

        PojoPropertySelectionModel that = (PojoPropertySelectionModel) o;

        if (labelField != null ? !labelField.equals(that.labelField) : that.labelField != null)
            return false;
        if (list != null ? !list.equals(that.list) : that.list != null)
            return false;

        return true;
    }

    public int hashCode() {
        int result;
        result = (list != null ? list.hashCode() : 0);
        result = 31 * result + (labelField != null ? labelField.hashCode() : 0);
        return result;
    }
}
